package com.qdsgjsfk.vision.adapter;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothGatt;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleGattCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.base.BaseApplication;
import com.qdsgjsfk.vision.ui.MyBlueTooth;
import com.qdsgjsfk.vision.ui.TypeActivity;
import com.rest.response.DeviceResponse;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.wj.android.colorcardview.CardView;
import jz.joyoung.robot.util.LogUtil;

/**
 * @time 2020/12/11 9:25
 */

public class BlueToothAdapter extends RecyclerView.Adapter<BlueToothAdapter.BlueToothHolder> {

    private Context mContext;
    private List<BleDevice> mBlueDevicesList;
    private OnItemClickLitener mOnItemClickLitener;

    public BlueToothAdapter(Context context, List<BleDevice> list) {
        this.mContext = context;
        this.mBlueDevicesList = list;
    }


    @NonNull
    @Override
    public BlueToothHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BlueToothHolder( LayoutInflater.from( mContext ).inflate( R.layout.item_bluetooth, parent, false ) );
    }


    @SuppressLint("RecyclerView")
    @Override
    public void onBindViewHolder(@NonNull BlueToothHolder holder, int position) {
        if (position == 0) {
            holder.container.setBackgroundResource( R.drawable.round_white_top );
            holder.line.setVisibility( View.VISIBLE );
        } else if (position == mBlueDevicesList.size() - 1) {
            holder.container.setBackgroundResource( R.drawable.round_white_bottom );
            holder.line.setVisibility( View.GONE );
        } else {
            holder.line.setVisibility( View.VISIBLE );
        }

//            String str = lstDevices.get(position).getName();
//            if (str == null | str.equals("")) {
//////                return;
//////            }
//////            String[] values = str.split("\\|");
//////            String address = values[1];
        holder.tvName.setText( mBlueDevicesList.get( position ).getName() );
        holder.itemView.setOnClickListener( v -> {
            if (mOnItemClickLitener != null) {
                mOnItemClickLitener.onItemClick( position );
            }
        } );

    }

    @Override
    public int getItemCount() {
        return mBlueDevicesList.size();
    }

    public interface OnItemClickLitener {
        void onItemClick(int position);
    }


    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    static class BlueToothHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_bluetooth)
        ImageView imgBluetooth;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.img)
        ImageView img;
        @BindView(R.id.line)
        View line;
        @BindView(R.id.container)
        CardView container;

        BlueToothHolder(View itemView) {
            super( itemView );
            ButterKnife.bind( this, itemView );
        }
    }


}
